package app.goods.info.views;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class RowHeaderRenderer extends JLabel implements TableCellRenderer, ListSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean header = false;
	private final ArrayList<Integer> col = new ArrayList<>();
	private final JTable reftable;
	private final JTable tableShow;
	private Color tableSeleBg;

	public RowHeaderRenderer(JTable reftable, JTable tableShow) {
		this.reftable = reftable;
		this.tableShow = tableShow;
		setOpaque(true);
		setBorder(javax.swing.UIManager.getBorder("TableHeader.cellBorder"));
		setHorizontalAlignment(LEFT);
		initConfig();
	}

	private void initConfig() {
		tableSeleBg = reftable.getSelectionBackground();
		DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
		dtcr.setHorizontalAlignment(SwingConstants.LEFT);
		reftable.setDefaultRenderer(Object.class, dtcr);
		reftable.getTableHeader().addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (col.isEmpty()) {
					header = true;
					tableShow.clearSelection();
					reftable.clearSelection();
				}
				int p = reftable.columnAtPoint(e.getPoint());
				header = true;
				if (col.contains(p)) {
					col.removeIf((t) -> {
						return t == p;
					});
					reftable.removeColumnSelectionInterval(p, p);
				} else {
					col.add(p);
					reftable.addColumnSelectionInterval(p, p);
					reftable.addRowSelectionInterval(0, reftable.getRowCount() - 1);
				}
			}
		});
		reftable.getTableHeader().addMouseMotionListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseMoved(java.awt.event.MouseEvent e) {
				int c = reftable.getColumnModel().getColumnIndexAtX(e.getX());
				if (c > -1) {
					String s = reftable.getColumnName(c);
					reftable.getTableHeader().setToolTipText(s == null || s.isEmpty() ? null : s);
				}
			}
		});
		reftable.addMouseMotionListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseMoved(java.awt.event.MouseEvent e) {
				int r = reftable.rowAtPoint(e.getPoint());
				int c = reftable.columnAtPoint(e.getPoint());
				if (r > -1 && c > -1) {
					Object o = reftable.getValueAt(r, c);
					reftable.setToolTipText(o == null || o.toString().isEmpty() ? null : o.toString());
				}
			}
		});
		reftable.getModel().addTableModelListener(new TableModelListener() {
			@Override
			public void tableChanged(TableModelEvent e) {
				if (e.getType() == javax.swing.event.TableModelEvent.DELETE
						|| e.getType() == javax.swing.event.TableModelEvent.INSERT) {
					((DefaultTableModel) tableShow.getModel()).setRowCount(reftable.getRowCount());
					tableShow.getSelectionModel().clearSelection();
					tableShow.revalidate();
				}
			}
		});
		ListSelectionModel lsm = reftable.getSelectionModel();
		lsm.addListSelectionListener(new ListSelectionListener() {

			private boolean flag = true;
			private int rowIndex;
			private int colIndex;

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (flag) {
					flag = false;
					colIndex = reftable.getSelectedColumnCount();
					rowIndex = lsm.getLeadSelectionIndex();
				}
				if (!e.getValueIsAdjusting()) {
					flag = true;
					if (header) {
						header = false;
					} else {
						if (colIndex > 1 || reftable.getSelectedRowCount() > 1) {
							return;
						}
						if (!col.isEmpty()) {
							col.clear();
						}
						if (rowIndex > -1) {
							tableShow.setRowSelectionInterval(rowIndex, rowIndex);
						}
					}
				}
			}
		});
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus,
			int row, int col) {
		((DefaultTableModel) table.getModel()).setRowCount(reftable.getRowCount());
		if (isSelected) {
			if (table.getSelectedRow() < reftable.getRowCount()) {
				reftable.setRowSelectionInterval(row, row);
				reftable.setColumnSelectionInterval(0, reftable.getColumnCount() - 1);
				setForeground(Color.WHITE);
				setBackground(tableSeleBg);
			}
		} else {
			setForeground(Color.BLACK);
			if (row % 2 == 0) {
				setBackground(Color.WHITE);
			} else {
				setBackground(reftable.getTableHeader().getBackground());
			}
		}
		setText(String.valueOf(row + 1));
		return this;
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		tableShow.revalidate();
	}
}
